﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyNhaSach.param
{
    public partial class FrmRuleObject : DevComponents.DotNetBar.Office2007Form
    {
        RuleObjectBUS RuleBUS;

        public FrmRuleObject()
        {
            InitializeComponent();
            RuleBUS = new RuleObjectBUS();
            dtgRule.AutoGenerateColumns = false;
            cmbUserGroup.DisplayMember = UserGroup.UsersGroupName.ToString();
            cmbUserGroup.ValueMember = UserGroup.UsersGroupID.ToString();
        }

        private void FrmRuleObject_Load(object sender, EventArgs e)
        {
            UsersGroupBUS UserBUS = new UsersGroupBUS();
            List<UsersGroupDTO> listUser = UserBUS.GetUsersGroupList();
            cmbUserGroup.DataSource = listUser;
            cmbUserGroup.SelectedValueChanged += new EventHandler(cmbUserGroup_SelectedIndexChanged);
            this.VisibleChanged += new EventHandler(FrmRuleObject_VisibleChanged);
        }

        void FrmRuleObject_VisibleChanged(object sender, EventArgs e)
        {
            
        }

        void cmbUserGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Global.intParse(cmbUserGroup.SelectedValue);
            List<RuleObjectDTO> listRule = RuleBUS.GetListRuleObject(id);
            dtgRule.DataSource = listRule;
            Global.GenerateNumber(dtgRule, colNumber.Index);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            List<RuleObjectDTO> listRule = new List<RuleObjectDTO>();
            
            foreach (DataGridViewRow row in dtgRule.Rows)
            {
                RuleObjectDTO rule = new RuleObjectDTO();
                rule.GroupID = Global.intParse(cmbUserGroup.SelectedValue);
                rule.RuleID = Global.intParse(row.Cells[colRuleID.Index].Value);
                rule.RuleName = Global.stringParse(row.Cells[colRuleName.Index].Value);
                rule.Status = bool.Parse(row.Cells[colStatus.Index].Value.ToString());

                listRule.Add(rule);
            }

            if (RuleBUS.UpdateRuleObject(listRule))
            {
                Global.SetMessage(lblMessage, "Phân quyền thành công!", true);
            }
            else
            {
                Global.SetMessage(lblMessage, "Có lỗi trong quá trình làm việc!", false);
            }
        }
    }
}
